package com.bjbn.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.bjbn.service.SPService;
import com.bjbn.util.HutoolTokenCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SPServiceImpl implements SPService {

    @Autowired
    private HutoolTokenCacheUtil tokenCacheUtil;

    @Value("${sp-config.base-url}")
    private String baseUrl;

    @Value("${sp-config.username}")
    private String username;

    @Value("${sp-config.password}")
    private String password;


    @Override
    public String getSpTokenFocus() {
        String tokenUrl = "VideoPlatform/Account/login";
        String url = baseUrl + tokenUrl;

        HttpRequest request = HttpUtil
                .createPost(url)
                .setConnectionTimeout(5000)
                .timeout(10000)
                .body("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}");


        // 发送POST请求并获取响应
        HttpResponse response = request.execute();

        JSONObject token = JSONObject.parseObject(response.body());

        String accessToken = token.getString("data");
        if (accessToken == null) {
            log.error("获取登录信息失败");
        }

        System.out.println("SP_TOKEN:" + accessToken);
        // 缓存token信息
        tokenCacheUtil.putToken("SP_TOKEN", accessToken);

        return accessToken;
    }

    @Override
    public String getSpToken() {
        if (tokenCacheUtil.containsToken("SP_TOKEN")) {
            return tokenCacheUtil.getToken("SP_TOKEN");
        }

        String tokenUrl = "VideoPlatform/Account/login";
        String url = baseUrl + tokenUrl;

        HttpRequest request = HttpUtil
                .createPost(url)
                .setConnectionTimeout(5000)
                .timeout(10000)
                .body("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}");


        // 发送POST请求并获取响应
        HttpResponse response = request.execute();

        String body = "{\n" +
                "  \"code\": 200,\n" +
                "  \"msg\": \"success\",\n" +
                "  \"data\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkJlYXJlciJ9.eyJwcmltYXJ5c2lkIjoiMTAxMCIsIm5hbWVpZCI6IjEwMTAiLCJ1bmlxdWVfbmFtZSI6ImJ5eGsiLCJncm91cHNpZCI6IjAiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3VzZXJkYXRhIjoie1wiVXNlcklkXCI6MTAxMCxcIkRlcHRJZFwiOjAsXCJVc2VyTmFtZVwiOlwiYnl4a1wiLFwiUm9sZUlkc1wiOltcImRzZmprXCJdLFwiUm9sZXNcIjpbe1wiUm9sZUlkXCI6NSxcIlJvbGVLZXlcIjpcImRzZmprXCIsXCJEYXRhU2NvcGVcIjoxfV0sXCJFeHBpcmVUaW1lXCI6XCIwMDAxLTAxLTAxVDAwOjAwOjAwXCJ9IiwiQXVkaWVuY2UiOiJaUkFkbWluLk5FVCIsIklzc3VlciI6IlpSQWRtaW4uTkVUIiwibmJmIjoxNzQ4MzA5NzU5LCJleHAiOjE3NDgzMTE1NTksImlhdCI6MTc0ODMwOTc1OSwiaXNzIjoiWlJBZG1pbi5ORVQiLCJhdWQiOiJaUkFkbWluLk5FVCJ9.0PXNcn5bDV0ZdbcX2ENniBQ56orVt1pR2MDhWMMjCHY\"\n" +
                "}";

        JSONObject token = JSONObject.parseObject(response.body());

        String accessToken = token.getString("data");
        if (accessToken == null) {
            log.error("获取登录信息失败");
        }

        System.out.println("SP_TOKEN:" + accessToken);
        // 缓存token信息
        tokenCacheUtil.putToken("SP_TOKEN", accessToken);

        return accessToken;
    }
}
